﻿/* 素数（质数）算法类
 */

namespace LSLib.Math
{
    using System.Collections;

    public class CalcPrimes
    {
        private long min;
        private long max;

        public CalcPrimes(long minimum, long maximum)
        {
            this.min = (minimum < 2) ? 2 : minimum;
            this.max = maximum;
        }

        public IEnumerator GetEnumerator()
        {
            for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
            {
                bool isPrime = true;
                for (long possibleFactor = 2; possibleFactor <= (long)System.Math.Floor(System.Math.Sqrt(possiblePrime)); possibleFactor++)
                {
                    long remainderAfterDivision = possiblePrime % possibleFactor;
                    if (remainderAfterDivision == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }

                if (isPrime) yield return possiblePrime;
            }
        }
    }
}